package cayte.sudoku.crack;

import com.umeng.analytics.MobclickAgent;
import com.umeng.update.UmengUpdateAgent;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
	public static enum State {
		NULL, START, CRACKING, STOP, END, RESTART, RESET, SUCCESS
	}

	private TableView table;
	private InputView input;
	private ClickListener clickListener;
	private Button crack, clear, reset;
	private TextView about;
	private LinearLayout inputPanel;
	private State state = State.NULL;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		SystemParams.newInstance(this);
		findView();
		init();
		// table.testData();

		// Umeng Update
		UmengUpdate();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}

	private void findView() {
		// TODO Auto-generated method stub
		inputPanel = (LinearLayout) findViewById(R.id.inputPanel);
		about = (TextView) findViewById(R.id.about);
		crack = (Button) findViewById(R.id.crack);
		clear = (Button) findViewById(R.id.clear);
		reset = (Button) findViewById(R.id.reset);
		table = new TableView(this);
		table.init(findViewById(R.id.table));
		input = new InputView();
		input.init(findViewById(R.id.input));
	}

	private void init() {
		// TODO Auto-generated method stub
		clickListener = new ClickListener();
		input.setOnClickListener(clickListener);
		crack.setOnClickListener(clickListener);
		clear.setOnClickListener(clickListener);
		reset.setOnClickListener(clickListener);
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		// TODO Auto-generated method stub
		super.onWindowFocusChanged(hasFocus);
		if (hasFocus) {
			showInput();
		}
	}

	private void showInput() {
		// TODO Auto-generated method stub
		if (inputPanel.getVisibility() == View.VISIBLE)
			return;
		int height = inputPanel.getMeasuredHeight() / 4
				- DisplayUtil.dip2px(2f);
		LinearLayout.LayoutParams btnLp = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT, height);
		btnLp.weight = 1;
		int margin = DisplayUtil.dip2px(1f);
		btnLp.setMargins(margin, margin, margin, margin);
		about.setLayoutParams(btnLp);
		crack.setLayoutParams(btnLp);
		clear.setLayoutParams(btnLp);
		reset.setLayoutParams(btnLp);
		input.initLp(height);
		inputPanel.setVisibility(View.VISIBLE);
	}

	private class ClickListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.input1:
			case R.id.input2:
			case R.id.input3:
			case R.id.input4:
			case R.id.input5:
			case R.id.input6:
			case R.id.input7:
			case R.id.input8:
			case R.id.input9:
				if (state == State.CRACKING || state == State.END)
					return;
				String input = v.getTag().toString();
				table.setValue(input);
			case R.id.inputNext:
				table.next();
				break;
			case R.id.crack:
				if (state == State.NULL)
					doCrack(State.START);
				else if (state == State.CRACKING)
					doCrack(State.STOP);
				else if (state == State.END)
					doCrack(State.RESTART);
				break;
			case R.id.clear:
				if (state == State.CRACKING || state == State.END)
					return;
				table.clear();
				break;
			case R.id.inputC:
				if (state == State.CRACKING || state == State.END)
					return;
				table.remove();
				break;
			case R.id.reset:
				if (state == State.END)
					doCrack(State.RESET);
				break;
			default:
				break;
			}
		}
	}

	public void doCrack(final State state) {
		// TODO Auto-generated method stub
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				switch (state) {
				default:
				case START:
					crack.setText(R.string.stopCrack);
					table.setClick(false);
					table.setSelect(null);
					MainActivity.this.state = State.CRACKING;
					doStartCrack();
					break;
				case STOP:
					table.stop();
					crack.setText(R.string.restart);
					table.setClick(false);
					MainActivity.this.state = State.END;
					break;
				case END:
					crack.setText(R.string.restart);
					table.setClick(false);
					MainActivity.this.state = State.END;
					break;
				case RESET:
					crack.setText(R.string.startCrack);
					table.reset();
					table.setClick(true);
					MainActivity.this.state = State.NULL;
					break;
				case RESTART:
					crack.setText(R.string.startCrack);
					table.setClick(true);
					table.clear();
					MainActivity.this.state = State.NULL;
					break;
				}
			}
		});
	}

	private void doStartCrack() {
		// TODO Auto-generated method stub
		new AsyncTask<String, String, String>() {
			@Override
			protected String doInBackground(String... params) {
				// TODO Auto-generated method stub
				table.crack();
				return null;
			}
		}.execute();
	}

	private void UmengUpdate() {
		UmengUpdateAgent.update(this);
		UmengUpdateAgent.setUpdateOnlyWifi(false);
		UmengUpdateAgent.setUpdateAutoPopup(true);
	}
}
